from peewee import *
from pyasn1.compat.octets import null

db = MySQLDatabase('nemtocr', host='localhost', port=3800, user='root', password='!@mtg23YWZ^&')


class UniversityPlanScoreError(Model):
    id = PrimaryKeyField()
    cid = IntegerField()
    year = IntegerField()
    page = IntegerField()
    batch = IntegerField()
    ucode = CharField(max_length=32)
    uname = CharField(max_length=64)
    major = CharField(max_length=64)
    major_type_2 = IntegerField()
    plan_number = IntegerField()
    submit_number = IntegerField()
    enter_number = IntegerField()
    score = TextField()
    remark = TextField()

    class Meta:
        database = db  # This model uses the "spider" database.
        table_name = 'university_plan_score_error'  # 如果不指定，默认是person


    # 插入单条数据(返回新增id)
    def toInsert(self, row):
        try:
            return UniversityPlanScoreError.insert(row).execute()
        except Exception as e:
            return ''


    # 批量插入数据
    def toInsertBatch(self, rows):
        with db.atomic():
            UniversityPlanScoreError.insert_many(rows).execute()


    # 根据id，获取单条记录
    def getOne(self, id):
        try:
            return UniversityPlanScoreError.select().where(UniversityPlanScoreError.id == id).get()
        except Exception as e:
            return ''


    # 根据年份查询列表
    def getList(self, year):
        try:
            return UniversityPlanScoreError.select().where(UniversityPlanScoreError.year == year).dicts().execute()
        except Exception as e:
            return ''


    # 更新单条数据(返回影响记录数)
    def toUpdate(self, row):
        try:
            return UniversityPlanScoreError.update(
                year=row['year'], batch=row['batch'],
                page=row['page'], ucode=row['ucode'], uname=row['uname'],
                major=row['major'], score=row['score'],
                plan_number=row['plan_number'], submit_number=row['submit_number'],
                enter_number=row['enter_number']
            ).where(UniversityPlanScoreError.cid == row['cid']).execute()
        except Exception as e:
            return ''


if __name__ == '__main__':
    for row in UniversityPlanScoreError.select():
        print(row.name, type(row))